
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author tkor
 */
public class Menu extends GameCanvas{

    
    private Image background;
    private Image shader;
    

    private Timer mTimer;
    private Graphics g;
    
    private Start start;
    private Settings settings;
    private Exit exit;
    private Hightscore hightscore;
    private Continue continue_;
    public Menudetail[] items;
    private int currentActiveItem = 0;
    public rwBC docghi = new rwBC();
    public boolean  check;
    public playmusic menumusic ;
    public Menu() {
        super(false);
        try {
            ImageLoader loader = ImageLoader.getInstance();
            background = loader.loadImage("/bg_menu.png");
            shader = loader.loadImage("/shader.png");
            menumusic = new playmusic("test-wav.wav");
        }catch(IOException io) {

        }
        setFullScreenMode(true);
        docghi.doc();
        if(docghi.check())
            check = true;
        if(check)
        {
            items = new Menudetail[5];
            continue_ = new Continue();
            continue_.setPos(110, 2);
            start = new Start();
            start.setPos(110, 62);
            hightscore = new Hightscore();
            hightscore.setPos(90, 122);
            settings = new Settings();
            settings.setPos(85, 182);
            exit = new Exit();
            exit.setPos(150, 242);//180

            items[1] = start;
            items[3] = settings;
            items[4] = exit;
            items[2] = hightscore;
            items[0] = continue_;
        }
        else
        {
             items = new Menudetail[4];
             start = new Start();
            start.setPos(110, 2);
            hightscore = new Hightscore();
            hightscore.setPos(90, 62);
            settings = new Settings();
            settings.setPos(85, 122);
            exit = new Exit();
            exit.setPos(150, 184);//180

            items[0] = start;
            items[1] =hightscore ;
            items[2] =settings ;
            items[3] = exit;

        }
    }

    public void showNotify() {
        
        menumusic.play("test-wav.wav");
        
        mTimer = new Timer();
        g = getGraphics();
        TimerTask ui = new TimerTask() {
          public void run()  {
              render(g);
              flushGraphics();
          }
        };
        mTimer.schedule(ui, 0, 30);
    }

    public void hideNotify() {
        menumusic.close();
        menumusic.player  = null;
        mTimer.cancel();
        
    }

    private void render(Graphics g) {
        int anchor = Graphics.TOP|Graphics.LEFT;
        g.drawImage(background, 0, 0, anchor);
        g.drawImage(shader, 0, 0, anchor);

        start.render(g);
        settings.render(g);
        exit.render(g);
        hightscore.render(g);
        if(check)
            continue_.render(g);
    }

    protected void pointerPressed(int x, int y) {
        start.pointerPressed(x, y);
        settings.pointerPressed(x, y);
        exit.pointerPressed(x, y);
        hightscore.pointerPressed(x, y);
        if(check) continue_.pointerPressed(x, y);
    }

    protected void pointerReleased(int x, int y) {
        start.pointerReleased(x,y);
        settings.pointerReleased(x,y);
        exit.pointerReleased(x,y);
        hightscore.pointerReleased(x, y);
       if(check) continue_.pointerReleased(x, y);
    }
    protected void keyPressed(int keyCode) {
        int max;
        if(check) max=4;
        else max=3;

            switch(keyCode){
                case GameCanvas.KEY_NUM2:
                {
                     items[currentActiveItem].setInactive();
                if(currentActiveItem == 0) {
                    currentActiveItem = max;
                }else {
                    currentActiveItem--;
                }
                items[currentActiveItem].setActive();
                }break;
                case GameCanvas.KEY_NUM8:
                {
                    items[currentActiveItem].setInactive();
                if(currentActiveItem == max) {
                    currentActiveItem = 0;
                }else {
                    currentActiveItem++;
                }
                items[currentActiveItem].setActive();
                }break;

                case GameCanvas.KEY_NUM5:
                {
                    items[currentActiveItem].setMenuItem();
                }break;
            }


    }



}
